package progetto3.managerAB;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ManagerLC extends ManagerAB {

	private Lock l = new ReentrantLock();
	private Condition c = l.newCondition();

	@Override
	public void request(MyRunnable r) {
		l.lock();
		try {
			while(!canPass(r)){
				w++;
				c.await();
				w--;
			}
			entered(r);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			l.unlock();
		}
	}

	@Override
	public void release(MyRunnable r) {
		l.lock();
		try {
			exited(r);
			if(w>0)
				c.signalAll();
		} finally {
			l.unlock();
		}
	}

}
